package com.controller.library;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.library.TGetSelfPapersAction;
import com.bean.library.StatisticPaper;
import com.bean.library.Teacher;

public class TNaviSelectServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(request.getSession().getAttribute("selectedCourseId")!=null && 
				request.getSession().getAttribute("selectedCourseName")!=null){
			request.getSession().removeAttribute("selectedCourseId");
			request.getSession().removeAttribute("selectedCourseName");
		}
		
		String operateType = request.getParameter("operate_type");
		String courseId = request.getParameter("course_id");
		String courseName = request.getParameter("course_name");
		if(operateType.equals("setques")){
			if(!courseId.equals("")){
				request.getSession().setAttribute("selectedCourseId", courseId);
				request.getSession().setAttribute("selectedCourseName", courseName);
			}
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_setques.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(operateType.equals("checkpaper")){
			if(!courseId.equals("")){
				request.getSession().setAttribute("selectedCourseId", courseId);
				request.getSession().setAttribute("selectedCourseName", courseName);
				Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
				String teacherId = teacher.getTeacherId();
				TGetSelfPapersAction tGetSelfPapersAction = new TGetSelfPapersAction();
				ArrayList<StatisticPaper>[] selfPapers = tGetSelfPapersAction.getselfPapers(teacherId, courseId);
				ArrayList<StatisticPaper> examPaperList = selfPapers[0];
				ArrayList<StatisticPaper> sparePaperList = selfPapers[1];
				ArrayList<StatisticPaper> modelPaperList = selfPapers[2];
				request.getSession().setAttribute("examPaperList", examPaperList);
				request.getSession().setAttribute("sparePaperList", sparePaperList);
				request.getSession().setAttribute("modelPaperList", modelPaperList);
				request.getSession().setAttribute("startIndex", 0);
			}
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_checkpaper.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			//正常情况不会执行到这里
		}
	}
}
